package baidu.com.matedemo.dlp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.duer.smartmate.out.DuerDevice;
import com.baidu.duer.smartmate.proxy.controller.ControllerManager;
import com.baidu.duer.smartmate.proxy.controller.DCSDataObserver;

import baidu.com.matedemo.base.BaseFragment;
import baidu.com.matedemo.R;
import baidu.com.matedemo.DemoApp;

/**
 * Created by niufei01 on 2018/2/6.
 * dlp协议支持三方开发者自定义协议
 */

public class ThirdPartyDemo extends BaseFragment implements DCSDataObserver<String> {

    public Button sendBtn;
    public EditText textView;
    ControllerManager controllerManager;

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.third_party_demo, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(View view) {
        DuerDevice duerDevice = DemoApp.getInstance().getCurreDuerDevice();
        controllerManager = duerDevice.getControllerManager();
        controllerManager.registerThirdPartyObserver(ThirdPartyDemo.this);

        this.sendBtn = (Button) view.findViewById(R.id.send_btn);
        this.textView = (EditText) view.findViewById(R.id.text_view);
        this.sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controllerManager.sendDataByThirdParty("hello dueros");
            }
        });
    }

    @Override
    public void onDataChanaged(String event, String message) {
        textView.append("event:");
        textView.append(event);
        textView.append("\n");
        textView.append("message:");
        textView.append(message);
    }

    @Override
    public void onDestroy() {
        if (controllerManager != null) {
            controllerManager.unregisterThirdPartyObserver(this);
        }
        super.onDestroy();
    }
}
